package jhavelogger.parser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jhavelogger.models.AcceptedConnection;
import jhavelogger.models.JHaveLog;
import jhavelogger.models.UidOperation;
import jhavelogger.models.UnparsedLog;

/**
 * Parses UID operation log message. This specifies which user will be performing
 * the following actions.
 * @author Jared Schlicht
 */
public class UidOperationFactory implements LogFactory {
    private Pattern pattern;

    public UidOperationFactory() {
	final String datePattern = "\\d{1,2}/\\d{1,2}/\\d{2}";
	final String timePattern = "\\d{1,2}:\\d{1,2} (?:AM|PM)";
	final String ipPattern = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";

	final String uidOperationRegex = "^("
		+ datePattern
		+ "\\s+"
		+ timePattern
		+ ")\\s+-\\s+INFO:\\s+/srv/www/htdocs/jhave/html_root/uid/(\\d+)\\s+\\*";
	this.pattern = Pattern.compile(uidOperationRegex, Pattern.CASE_INSENSITIVE);
    }

    @Override
    public JHaveLog create(UnparsedLog log) {
	Matcher matcher = this.pattern.matcher(log.getMessage());
	if (matcher.find()) {
	    String date = matcher.group(1);
	    int uid = Integer.parseInt(matcher.group(2));

	    UidOperation logObject = new UidOperation(log.getSource(), uid);
	    return logObject;
	}

	return null;
    }
}
